package csc.daonjpa.java.dao;

import java.util.List;





import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.TargetAccount;
import csc.daonjpa.java.domain.TargetAccount;

/**
 * DAO class for Account entity. This class contains all methods that
 * inserts/updates/deletes account info
 * 
 * @author nvu3
 *
 */
@Component
public class TargetAccountDAO {

	/**
	 * @return all accounts from Account table
	 */
	public List<TargetAccount> getTargetAccounts(String loginId) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		List<TargetAccount> accounts = null;
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<TargetAccount> query = entityManager.createQuery(
					"SELECT c FROM "
					+ TargetAccount.class.getName()
					+ "  c ", TargetAccount.class);		
			accounts = query.getResultList();
			entr.commit();
		} catch (Exception e) {
			System.out.println(" " + e.getMessage() + "Get all targetaccount");
			entityManager.close();
		}
		// -----------End transaction-----------
		return accounts;
	}

	public List<TargetAccount> getTargetAccountInfo(String accNumber) {
		
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil.createEntityManager();
		
		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();
		
		// -----------Begin transaction-----------
		List<TargetAccount> listAcc = null;
		try {
			entr.begin();
			// Get a list of accounts from DB
			
			TypedQuery<TargetAccount> query = entityManager.createQuery("SELECT c FROM " + TargetAccount.class.getName() + " c Where c.accountNumber= :accNumber",TargetAccount.class);
			query.setParameter("accNumber", accNumber);
			
			listAcc = query.getResultList();
			
			
			
			entr.commit();
		} catch (Exception e) {
			System.out.println(" " + e.getMessage());
			entityManager.close();
		}
		// -----------End transaction-----------

		return listAcc;
		
	}

}
